package com.marketingexpert.controller.messagemgnt;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;

public class GetShortUrl implements Controller {

	private SnsServiceFactory snsServiceFactory;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/text;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  	
		
		String url = request.getParameter("url");		
		SnsService snsService = getSnsServiceFactory().getService(SnsServiceFactory.SINA);
		PrintWriter out = response.getWriter();
		try {
		String shortUrl = snsService.shortUrl(url);
		
		
		out.print(shortUrl + " ");
		return null;
		} catch(Exception e) {
			out.print("error");
			e.printStackTrace();
			return null;
		}
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}
	
	

}
